using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace AppManagement.Repositories
{
	#region using
	using AppManagement.Models;
	using AppManagement.Helpers;
	#endregion

	public class LichHoatDongRepository
	{
		public static List<LichHoatDong> SelectAll()
		{
			try
			{
				return DataContext.Instance.LichHoatDong.ToList();
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return new List<LichHoatDong>();
		}

		public static LichHoatDong SelectByID(int malichhoatdong)
		{
			try
			{
				return DataContext.Instance.LichHoatDong.FirstOrDefault(item => item.MaLichHoatDong == malichhoatdong );
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return null;
		}

		public static bool Insert(LichHoatDong obj)
		{
			try
			{
				DataContext.Instance.LichHoatDong.AddObject(obj);
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
				return false;
		}

		public static bool Delete(int malichhoatdong)
		{
			try
			{
				var delitem = DataContext.Instance.LichHoatDong.FirstOrDefault(item => item.MaLichHoatDong == malichhoatdong );
				DataContext.Instance.LichHoatDong.DeleteObject(delitem);
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
				return false;
		}

		public static bool Save()
		{
			try
			{
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
				return false;
		}

		public static List<LichHoatDong> RetrieveByID(int malichhoatdong)
		{
			try
			{
				return (from item in DataContext.Instance.LichHoatDong where  item.MaLichHoatDong == malichhoatdong  select item).ToList();
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return new List<LichHoatDong>();
		}

		public static List<LichHoatDong> SelectByMaChuyenXe(int machuyenxe)
		{
			try
			{
			var lstItem = (from item in DataContext.Instance.LichHoatDong where item.MaChuyenXe == machuyenxe select item).ToList();
			return lstItem;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return new List<LichHoatDong>();
		}

        /// <summary>
        /// Select by ma chuyen xe, ngay bat dau, ngay ket thuc
        /// </summary>
        /// <param name="machuyenxe"></param>
        /// <param name="ngaybd"></param>
        /// <param name="ngaykt"></param>
        /// <returns></returns>
        public static LichHoatDong SelectByMaChuyenXeNgayBDNgayKT(int machuyenxe, DateTime ngaybd, DateTime ngaykt)
        {
            try
            {
                return DataContext.Instance.LichHoatDong.FirstOrDefault(
                                    n => n.MaChuyenXe == machuyenxe &&
                                    n.NgayBatDau.Value.Equals(ngaybd) &&
                                    n.NgayKetThuc.Value.Equals(ngaykt));
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    GlobalVariables.Logger.Error(ex.InnerException.Message);
                }
                else
                {
                    GlobalVariables.Logger.Error(ex.Message);
                }
            }
            return null;
        }

        /// <summary>
        /// Select by ma chuyen xe, and range of dates
        /// </summary>
        /// <param name="machuyenxe"></param>
        /// <param name="ngaybd"></param>
        /// <param name="ngaykt"></param>
        /// <returns></returns>
        public static LichHoatDong SelectByMaChuyenXeAndRangeDate(int machuyenxe, DateTime ngaybd, DateTime ngaykt)
        {
            try
            {
                return DataContext.Instance.LichHoatDong.FirstOrDefault(
                                    n => n.MaChuyenXe == machuyenxe &&
                                    n.NgayBatDau.Value >= ngaybd &&
                                    n.NgayKetThuc.Value <= ngaykt);
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    GlobalVariables.Logger.Error(ex.InnerException.Message);
                }
                else
                {
                    GlobalVariables.Logger.Error(ex.Message);
                }
            }
            return null;
        }

        /// <summary>
        /// Select by ngay bat dau and ngay ket thuc
        /// </summary>
        /// <param name="ngaybd"></param>
        /// <param name="ngaykt"></param>
        /// <returns></returns>
        public static LichHoatDong SelectByNgayBDNgayKT(DateTime ngaybd, DateTime ngaykt)
        {
            try
            {
                return DataContext.Instance.LichHoatDong.FirstOrDefault(n =>
                                    n.NgayBatDau.Value.Equals(ngaybd) &&
                                    n.NgayKetThuc.Value.Equals(ngaykt));
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    GlobalVariables.Logger.Error(ex.InnerException.Message);
                }
                else
                {
                    GlobalVariables.Logger.Error(ex.Message);
                }
            }
            return null;
        }
	}

}
